package com.jiyinit.web.core.mobile.controller;

import com.jiyinit.core.framework.base.entity.ResponseDto;
import com.jiyinit.core.framework.base.util.JsonEntityTransform;
import com.jiyinit.core.framework.base.util.LogUtil;
import com.jiyinit.web.core.business.dto.FriendGroupRequest;
import com.jiyinit.web.core.business.dto.FriendRequest;
import com.jiyinit.web.core.business.entity.TabCoreOrgEntity;
import com.jiyinit.web.core.business.entity.TabCoreUserEntity;
import com.jiyinit.web.core.business.entity.TabFriendGroupEntity;
import com.jiyinit.web.core.business.entity.TabUserFriendEntity;
import com.jiyinit.web.core.business.enums.CommonEnum;
import com.jiyinit.web.core.business.service.UserFriendServiceImpl;
import com.jiyinit.web.core.common.logic.ParamCheckLogic;
import com.jiyinit.web.core.common.logic.ValidateResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * Created by changly on 2017/12/6.
 */
@Controller
@RequestMapping("/mobile/friend")
public class MuserFriendController {
    /**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(MuserFriendController.class);

    @Resource(name = "core.paramCheckLogic")
    private ParamCheckLogic paramCheckLogic;

    @Resource(name = "core.friendServiceImpl")
    private UserFriendServiceImpl userFriendService;

    /**
     * 申请的好友请求
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "findAppfriend")
    public ResponseEntity<ResponseDto> findAppfriend(HttpServletRequest request, @RequestParam String request_json) {
        LogUtil.info(LOGGER, "接口:{}", "/mobile/friend/findAppfriend");
        String logJson = request.getParameter("request_json");
        LogUtil.info(LOGGER, "logJson:{}", logJson);
        FriendRequest friendRequest = (FriendRequest) JsonEntityTransform.json2Object(logJson, FriendRequest.class);
        // 校验参数
        ValidateResult<FriendRequest> validateResult = paramCheckLogic.checkParamValidate(friendRequest);
        if (!validateResult.isSuccess()) {
            return new ResponseEntity<>(validateResult.getDto(), HttpStatus.OK);
        }
        //人员信息
        List<TabUserFriendEntity> friends = userFriendService.findAppFriends(friendRequest.getUserId());

        LogUtil.info(LOGGER, "接口:/mobile/friend/findAppfriend,返回结果{}", JsonEntityTransform.object2Json(friends));
        return new ResponseEntity<>(ResponseDto.responseOK(friends), HttpStatus.OK);
    }

    /**
     * 好友查找
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "findAllfriend")
    public ResponseEntity<ResponseDto> findAllfriend(HttpServletRequest request) {
        LogUtil.info(LOGGER, "接口:{}", "/mobile/friend/findAllfriend");
        String logJson = request.getParameter("request_json");
        LogUtil.info(LOGGER, "logJson:{}", logJson);
        FriendRequest friendRequest = (FriendRequest) JsonEntityTransform.json2Object(logJson, FriendRequest.class);
        // 校验参数
        ValidateResult<FriendRequest> validateResult = paramCheckLogic.checkParamValidate(friendRequest);
        if (!validateResult.isSuccess()) {
            return new ResponseEntity<>(validateResult.getDto(), HttpStatus.OK);
        }
        //超找群组信息
        List<TabFriendGroupEntity> group =userFriendService.findGroupInfo(friendRequest.getUserId());
        //人员信息
        List<TabUserFriendEntity> friends = userFriendService.findFriends(friendRequest.getUserId());
        HashMap<String,Object> result = new HashMap<>();
        for (TabFriendGroupEntity entity : group){
            if(entity != null && entity.getNum() != null && entity.getNum().intValue() != 0 && entity.getFriendGroupId() == null && entity.getFriendGroupName() == null){
                entity.setFriendGroupName("未分组好友");
                entity.setFriendGroupId(0);
            }
        }
        result.put("group",group);
        result.put("friends",friends);
        LogUtil.info(LOGGER, "接口:/mobile/friend/findAllfriend,返回结果{}", JsonEntityTransform.object2Json(result));
        return new ResponseEntity<>(ResponseDto.responseOK(result), HttpStatus.OK);
    }

    /**
     * 加好友
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("addFriend")
    public ResponseEntity<ResponseDto> addFriend(HttpServletRequest request){
        LogUtil.info(LOGGER, "接口:{}", "/mobile/friend/addFriend");
        String logJson = request.getParameter("request_json");
        LogUtil.info(LOGGER, "logJson:{}", logJson);
        FriendRequest friendRequest = (FriendRequest) JsonEntityTransform.json2Object(logJson, FriendRequest.class);
        // 校验参数
        ValidateResult<FriendRequest> validateResult = paramCheckLogic.checkParamValidate(friendRequest);
        if (!validateResult.isSuccess()) {
            return new ResponseEntity<>(validateResult.getDto(), HttpStatus.OK);
        }
        if(friendRequest.getFriendId() == null || "".equals(friendRequest.getFriendId())){
            return new ResponseEntity<ResponseDto>(ResponseDto.responseFail("朋友ID不能为空"),HttpStatus.OK);
        }
        if(friendRequest.getUserId().intValue()==friendRequest.getFriendId().intValue()){
            return  new ResponseEntity<ResponseDto>(ResponseDto.responseFail("不能添加自己为好友"),HttpStatus.OK);
        }
        //是否已是好友
        List<TabUserFriendEntity> list = userFriendService.isFriend(friendRequest.getUserId(),friendRequest.getFriendId());
        for(TabUserFriendEntity entity: list){
            if(entity.getStatus().intValue()== CommonEnum.FriendStatus.WAIT.getCode().intValue())
                if(friendRequest.getUserId().intValue() == entity.getCreateId().intValue()){
                    return  new ResponseEntity<ResponseDto>(ResponseDto.responseFail("已申请加好友,等待对方同意"),HttpStatus.OK);
                }else{
                    return  new ResponseEntity<ResponseDto>(ResponseDto.responseFail("对方已申请加好友,去好友请求列表，点击同意"),HttpStatus.OK);
                }

            else if(entity.getStatus().intValue()== CommonEnum.FriendStatus.APPROVE.getCode().intValue()){
                return  new ResponseEntity<ResponseDto>(ResponseDto.responseFail("已是好友"),HttpStatus.OK);
            }
        }
        //添加为好友
        int num = userFriendService.addFriend(friendRequest);
        if(num > 0){
            return new ResponseEntity<ResponseDto>(ResponseDto.responseOK(""),HttpStatus.OK);
        }
        return  new ResponseEntity<ResponseDto>(ResponseDto.responseFail("添加好友失败"),HttpStatus.OK);
    }
    /**
     * 好友同意
     */
    @ResponseBody
    @RequestMapping("agreeFriend")
    public ResponseEntity<ResponseDto> agreeFriend(HttpServletRequest request){
        LogUtil.info(LOGGER, "接口:{}", "/mobile/friend/agreeFriend");
        String logJson = request.getParameter("request_json");
        LogUtil.info(LOGGER, "logJson:{}", logJson);
        FriendRequest friendRequest = (FriendRequest) JsonEntityTransform.json2Object(logJson, FriendRequest.class);
        // 校验参数
        ValidateResult<FriendRequest> validateResult = paramCheckLogic.checkParamValidate(friendRequest);
        if (!validateResult.isSuccess()) {
            return new ResponseEntity<>(validateResult.getDto(), HttpStatus.OK);
        }
        if(friendRequest.getFriendId() == null || "".equals(friendRequest.getFriendId())){
            return new ResponseEntity<ResponseDto>(ResponseDto.responseFail("朋友ID不能为空"),HttpStatus.OK);
        }

        //修改状态
        int num = userFriendService.agreeFriend(friendRequest);
        if(num > 0){
            return new ResponseEntity<ResponseDto>(ResponseDto.responseOK(""),HttpStatus.OK);
        }
        return  new ResponseEntity<ResponseDto>(ResponseDto.responseFail("添加好友失败"),HttpStatus.OK);
    }

    /**
     * 新建好友分组
     */
    @ResponseBody
    @RequestMapping("addFriendGroup")
    public ResponseEntity<ResponseDto> addFriendGroup(HttpServletRequest request){
        LogUtil.info(LOGGER, "接口:{}", "/mobile/friend/addFriendGroup");
        String logJson = request.getParameter("request_json");
        LogUtil.info(LOGGER, "logJson:{}", logJson);
        FriendGroupRequest groupRequest = (FriendGroupRequest) JsonEntityTransform.json2Object(logJson, FriendGroupRequest.class);
        // 校验参数
        ValidateResult<FriendGroupRequest> validateResult = paramCheckLogic.checkParamValidate(groupRequest);
        if (!validateResult.isSuccess()) {
            return new ResponseEntity<>(validateResult.getDto(), HttpStatus.OK);
        }
        if(groupRequest.getFriendGroupName() == null || "".equals(groupRequest.getFriendGroupName())){
            return new ResponseEntity<ResponseDto>(ResponseDto.responseFail("组名称不能为空"),HttpStatus.OK);
        }

        //添加分组
        int groupId = userFriendService.addFriendGroup(groupRequest);
        if(groupId > 0){
            return new ResponseEntity<ResponseDto>(ResponseDto.responseOK(groupId),HttpStatus.OK);
        }
        return  new ResponseEntity<ResponseDto>(ResponseDto.responseFail("添加分组失败"),HttpStatus.OK);
    }

    /**
     * 好友分组添加成员
     */
    @ResponseBody
    @RequestMapping("addGroupMember")
    public ResponseEntity<ResponseDto> addGroupMember(HttpServletRequest request){
        LogUtil.info(LOGGER, "接口:{}", "/mobile/friend/addGroupMember");
        String logJson = request.getParameter("request_json");
        LogUtil.info(LOGGER, "logJson:{}", logJson);
        FriendGroupRequest groupRequest = (FriendGroupRequest) JsonEntityTransform.json2Object(logJson, FriendGroupRequest.class);
        // 校验参数
        ValidateResult<FriendGroupRequest> validateResult = paramCheckLogic.checkParamValidate(groupRequest);
        if (!validateResult.isSuccess()) {
            return new ResponseEntity<>(validateResult.getDto(), HttpStatus.OK);
        }
        if(groupRequest.getUserId() == null || "".equals(groupRequest.getUserId())){
            return new ResponseEntity<ResponseDto>(ResponseDto.responseFail("用户ID不能为空"),HttpStatus.OK);
        }
        if(groupRequest.getFriendGroupId()== null || "".equals(groupRequest.getFriendGroupId())){
            return new ResponseEntity<ResponseDto>(ResponseDto.responseFail("好友分组ID不能为空"),HttpStatus.OK);
        }

        //添加分组
        int num = userFriendService.updateUserFriend(groupRequest);
        if(num > 0){
            return new ResponseEntity<ResponseDto>(ResponseDto.responseOK(""),HttpStatus.OK);
        }
        return  new ResponseEntity<ResponseDto>(ResponseDto.responseFail("添加人员失败"),HttpStatus.OK);
    }


}
